<?php


if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
if(!session_id()) session_start();

require_once libfile('function/member');

function AcanOpenGetHost(){
	return 'http://open.rssmv.com/';
}
function getToken(){
	global $_G;
	loadcache('acan_open_token');
	$rs = json_decode($_G['cache']['acan_open_token']);
	if($rs->token && $rs->start + 7000 > $_SERVER['REQUEST_TIME']){
		return $rs->token;
	}
	$openOpt = $_G['cache']['plugin']['acan_open'];
	$url=AcanOpenGetHost()."qrcode/create_token?key=".$openOpt['key'].'&secret='.$openOpt['secret'];
	$rs=Adz::curl('get',$url,'',1);
	if($rs && $rs['data']){
		$rs=json_decode($rs['data']);
		if($rs && $rs->token){
			$rs->start=$_SERVER['REQUEST_TIME'];
			savecache('acan_open_token', json_encode($rs));
			return $rs->token;
		}else{
			return '';
		}
	}else{
		return '';
	}
}
function createLogin(){
	global $_G;
	$openOpt = $_G['cache']['plugin']['acan_open'];
	$url=AcanOpenGetHost()."qrcode/create_login?key=".$openOpt['key'].'&token='.getToken().'&sid='.$_GET['sid'];
	$rs=Adz::curl('get',$url,'',1);
	return $rs;
}
function createReLogin(){
	global $_G;
	$openOpt = $_G['cache']['plugin']['acan_open'];
	$url=AcanOpenGetHost()."qrcode/create_relogin?key=".$openOpt['key'].'&token='.getToken().'&sid='.$_GET['sid'].'&uid='.$_G['uid'];
	$rs=Adz::curl('get',$url,'',1);
	return $rs;
}
function createBind(){
	global $_G;
	$openOpt = $_G['cache']['plugin']['acan_open'];
	$url=AcanOpenGetHost()."qrcode/create_bind?key=".$openOpt['key'].'&token='.getToken().'&sid='.$_GET['sid'].'&uid='.$_G['uid'].'&nickname='.$_G['username'];
	$rs=Adz::curl('get',$url,'',1);
	return $rs;
}
function checkBind(){
	global $_G;
	$openOpt = $_G['cache']['plugin']['acan_open'];
	$ckey='acan_open_bind_'+$_G['uid'];
	loadcache($ckey);
	$rs = $_G['cache'][$ckey];
	if($rs){
		return $rs;
	}
	$url=AcanOpenGetHost()."qrcode/check_bind?key=".$openOpt['key'].'&token='.getToken().'&uid='.$_G['uid'];
	$rs=Adz::curl('get',$url,'',1);
	$rs = json_decode($rs['data']);
	if(isset($rs->status)){
		savecache('acan_open_token', $rs->status);
		return $rs->status;
	}else{
		return -1;
	}
}

function acan_py($act=''){
	global $_G;
	$rt = true;
	$auth = "http://wxauth.nice9s.com:8080/";
	if(empty($act)){$act=$_GET['act'];$rt=false;}
	if($act=='login'){
		$gets = $_GET;
		unset($gets['id'],$gets['act']);
		foreach ($gets as $k => $v) {
			$get .= "$k=$v&";
		}
	}else{$get='';}
	$url = $auth.$act.'?'.$get.'&Asid='.session_id().'&email='.$_G['cache']['plugin']['acan_open']['email'];
	$rs = Adz::curl('get',$url,'',1);
	if($act=='userinfo'){
		$data = json_decode($rs['data']);
		$result = $data;
	}elseif($act=='login'){
		$data = json_decode($rs['data']);
		header('Content-Type: text/javascript');
		if($data->status==1){
			echo 'window.location.href="'.$_G['cache']['plugin']['acan_open']['callback'].'";';
		}else{
			echo 'alert("'.lang('plugin/acan_open', 'error_login').'");window.location.reload();';
		}
		return;
	}else{
		echo $rs['data'];return;
	}
	if($rt) return $result;
	else json_encode($result);
}

// uc同步登录
function manageaftlogin($uid) {
	global $_G;
	DB::update(('common_member_status'), array('lastip'=>$_G['clientip'], 'lastvisit'=>TIMESTAMP, 'lastactivity' => TIMESTAMP), 'uid=\''.$uid.'\'');
	$ucsynlogin = '';
	if($_G['setting']['allowsynlogin']) {
		loaducenter();
		$ucsynlogin = uc_user_synlogin($_G['uid']);
	}
}
// 登录discuz
function connect_login($uid) {
	global $_G;
	$member = DB::fetch_first('SELECT * FROM '.DB::table('common_member')." WHERE uid='$uid'");
	
	if(!($member = getuserbyuid($uid, 1))) {
		return false;
	} else {
		if(isset($member['_inarchive'])) {
			C::t('common_member_archive')->move_to_master($member['uid']);
		}
	}
	$cookietime = 2592000;
	setloginstatus($member, $cookietime);
	return true;
}
function addbindinfo($fields) {
	return DB::insert('plugin_weixin', $fields);
}
function acan_fetch_uid($username){
	$sql = 'SELECT uid FROM '.DB::table('common_member').' WHERE username=\''.$username.'\'';
	$tmp = DB::fetch_first($sql);
	return empty($tmp) ? 0 : $tmp['uid'];
}
function addmember($save, $extdata) {
	global $_G;
	$credits = isset($extdata['credits']) ? $extdata['credits'] : array();
	$profile = isset($extdata['profile']) ? $extdata['profile'] : array();
	$ip = $_SERVER['REMOTE_ADDR'];
	$uid = $save['uid'];
	$save['password']=md5(rand(100000,999999));
	$save['regdate'] = TIMESTAMP;
	$save['emailstatus'] = intval($extdata['emailstatus']);
	$save['credits'] = intval($credits[0]);
	$save['timeoffset'] = 9999;
	$status = array(
		'uid' => $uid,
		'regip' => (string)$ip,
		'lastip' => (string)$ip,
		'lastvisit' => TIMESTAMP,
		'lastactivity' => TIMESTAMP,
		'lastpost' => 0,
		'lastsendmail' => 0
	);
	$count = array(
		'uid' => $uid,
		'extcredits1' => intval($credits[1]),
		'extcredits2' => intval($credits[2]),
		'extcredits3' => intval($credits[3]),
		'extcredits4' => intval($credits[4]),
		'extcredits5' => intval($credits[5]),
		'extcredits6' => intval($credits[6]),
		'extcredits7' => intval($credits[7]),
		'extcredits8' => intval($credits[8])
	);
	$profile['uid'] = $uid;
	$field_forum['uid'] = $uid;
	$field_home['uid'] = $uid;
	DB::insert('common_member', $save, true);
	DB::insert('common_member_status', $status, true);
	DB::insert('common_member_count', $count, true);
	DB::insert('common_member_profile', $profile, true);
	DB::insert('common_member_field_forum', $field_forum, true);
	DB::insert('common_member_field_home', $field_home, true);
	DB::insert('common_setting', array('skey' => 'lastmember', 'svalue' => $save['username']), false, true);
	manyoulog('user', $uid, 'add');
	$totalmembers = DB::result_first("SELECT COUNT(*) FROM ".DB::table('common_member'));
	$userstats = array('totalmembers' => $totalmembers, 'newsetuser' => stripslashes($save['username']));
	save_syscache('userstats', $userstats);
}
?>